package edu.fiu.accesslog;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import nl.bitwalker.useragentutils.UserAgent;

public class SingleAccessLog
{

      private String remoteIpAddr;
      private String localIpAddr;
      private long bytesSent;
      private String remoteHostName;
      private String requestProtocol;
      private String remoteLogicalUserName;
      private String requestMethod;
      private String localPort;
      private String queryString;
      private String firstLineOfRequest;
      private String httpStatusCode;
      private String userSessionId;
      private Date dateTime;
      private String remoteUser;
      private String requestedUrlPath;
      private String localServerName;
      private String timeTakeInMilliSec;
      private String timeTakeInSec;
      private String currentRequestThreadName;
      private String userAgentString;
      private String referer;
      
      boolean userAgentParsed = false;
      private UserAgent userAgent;

      public SingleAccessLog()
      {
              bytesSent =0;
      }
      public String getRemoteIpAddr()
      {
            return remoteIpAddr;
      }

      public void setRemoteIpAddr(String remoteIpAddr)
      {
            this.remoteIpAddr = remoteIpAddr;
      }

      public String getLocalIpAddr()
      {
            return localIpAddr;
      }

      public void setLocalIpAddr(String localIpAddr)
      {
            this.localIpAddr = localIpAddr;
      }

      public long getBytesSent()
      {
            return bytesSent;
      }

      public void setBytesSent(long bytesSent)
      {
            this.bytesSent = bytesSent;
      }

      public String getRemoteHostName()
      {
            return remoteHostName;
      }
      
      public void setRemoteHostName(String remoteHostName)
      {
            this.remoteHostName = remoteHostName;
      }

      public String getRequestProtocol()
      {
            return requestProtocol;
      }

      public void setRequestProtocol(String requestProtocol)
      {
            this.requestProtocol = requestProtocol;
      }

      public String getRemoteLogicalUserName()
      {
            return remoteLogicalUserName;
      }

      public void setRemoteLogicalUserName(String remoteLogicalUserName)
      {
            this.remoteLogicalUserName = remoteLogicalUserName;
      }

      public String getRequestMethod()
      {
            return requestMethod;
      }

      public void setRequestMethod(String requestMethod)
      {
            this.requestMethod = requestMethod;
      }

      public String getLocalPort()
      {
            return localPort;
      }

      public void setLocalPort(String localPort)
      {
            this.localPort = localPort;
      }

      public String getQueryString()
      {
            return queryString;
      }

      public void setQueryString(String queryString)
      {
            this.queryString = queryString;
      }

      public String getFirstLineOfRequest()
      {
            return firstLineOfRequest;
      }

      public void setFirstLineOfRequest(String firstLineOfRequest)
      {
            this.firstLineOfRequest = firstLineOfRequest;
      }

      public String getHttpStatusCode()
      {
            return httpStatusCode;
      }

      public void setHttpStatusCode(String httpStatusCode)
      {
            this.httpStatusCode = httpStatusCode;
      }

      public String getUserSessionId()
      {
            return userSessionId;
      }

      public void setUserSessionId(String userSessionId)
      {
            this.userSessionId = userSessionId;
      }

      public static final String dateFormat = "[dd/MMM/yyyy:HH:mm:ssZ]";
      public Date getDateTime()
      {
            return dateTime;
      }
      public String getDateTimeString()
      {
            SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);            
            return formatter.format(dateTime);
      }
      
      public void setDateTime(String dateTime)
      {

            
            SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
            try
            {
                  this.dateTime =formatter.parse(dateTime);

            }
            catch (ParseException ex)
            {
                  ex.printStackTrace();
            }


            
      }

      public String getRemoteUser()
      {
            return remoteUser;
      }

      public void setRemoteUser(String remoteUser)
      {
            this.remoteUser = remoteUser;
      }

      public String getRequestedUrlPath()
      {
            return requestedUrlPath;
      }

      public void setRequestedUrlPath(String requestedUrlPath)
      {
            this.requestedUrlPath = requestedUrlPath;
      }

      public String getLocalServerName()
      {
            return localServerName;
      }

      public void setLocalServerName(String localServerName)
      {
            this.localServerName = localServerName;
      }

      public String getTimeTakeInMilliSec()
      {
            return timeTakeInMilliSec;
      }

      public void setTimeTakeInMilliSec(String timeTakeInMilliSec)
      {
            this.timeTakeInMilliSec = timeTakeInMilliSec;
      }

      public String getTimeTakeInSec()
      {
            return timeTakeInSec;
      }

      public void setTimeTakeInSec(String timeTakeInSec)
      {
            this.timeTakeInSec = timeTakeInSec;
      }

      public String getCurrentRequestThreadName()
      {
            return currentRequestThreadName;
      }

      public void setCurrentRequestThreadName(String currentRequestThreadName)
      {
            this.currentRequestThreadName = currentRequestThreadName;
      }

      @Override
      public String toString()
      {
            return "AccessLog ["
                    + " remoteHostName=" + remoteHostName 
                    + ", dateTime=" + getDateTimeString()
                    + ", requestMethod=" + requestMethod
                    + ", firstLineOfRequest=" + firstLineOfRequest
                    + ", requestProtocol=" + requestProtocol 
                    + ", httpStatusCode=" + getHttpStatusCode() 
                    + ", bytesSent=" + bytesSent
                     /*
                    + ", localPort=" + localPort                                      
                    +", remoteIpAddr=" + remoteIpAddr 
                    + ", localIpAddr=" + localIpAddr                   
                    + ", remoteLogicalUserName=" + remoteLogicalUserName
                    + ", queryString=" + queryString
                    + ", userSessionId="+ userSessionId 
                    + ", remoteUser=" + remoteUser 
                    + ", requestedUrlPath=" + requestedUrlPath
                    + ", localServerName=" + localServerName
                    + ", timeTakeInMilliSec=" + timeTakeInMilliSec
                    + ", timeTakeInSec=" + timeTakeInSec
                    + ", currentRequestThreadName=" + currentRequestThreadName
                    */
                    + "]";
      }

        /**
         * @return the userAgentString
         */
        public String getUserAgentString()
        {
                return userAgentString;
        }

        /**
         * @param userAgentString the userAgentString to set
         */
        public void setUserAgentString(String userAgentString )
        {
                this.userAgentString = userAgentString ;
                userAgentParsed = false;
        }

        /**
         * @return the referer
         */
        public String getReferer()
        {
                return referer;
        }

        /**
         * @param referer the referer to set
         */
        public void setReferer(String referer)
        {
                this.referer = referer;
        }

        /**
         * @return the userAgent
         */
        public UserAgent getUserAgent()
        {
                if( ! userAgentParsed)
                {
                        userAgentParsed = true;
                        try
                        {
                                userAgent = UserAgent.parseUserAgentString(userAgentString);
                        }
                        catch (Exception e)
                        {
                                
                        }
                }
                return userAgent;
        }
}
